import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/res/gaps.dart';

import '../../model/leave/leave_model.dart';
import '../../net/response/leave_record_list.dart';
import '../../res/colors.dart';

class FinishLeaveViewModel extends BaseViewModel{
  final LeaveRecordList item;
  FinishLeaveViewModel(BuildContext context,{required this.item}) : super(context);

  var backTime = "";
  var format = [yyyy,'-',mm,'-',dd,' ',HH,':',nn,':',ss];

  var leaveModel = LeaveModel();
  var leaveDays = '';
  var remark = "";

  @override
  void onCreated() {
    backTime = DateTime.now().toDateStr(format);
    super.onCreated();
    notifyListeners();
    getLeaveDays();
  }


  Widget contentView(){

    return Column(
      children: [
        ListView(
          children: [
            Row(
              children: [
                CommonText("${item.customerElderName}",textSize: 18, fontWeight: FontWeight.bold),
                Container(
                  width: 12,
                ),
                CommonText("${item.buildingNo}-${item.roomNumber}", textSize: 16)
              ],
            ),
            Container(
               height: 15
            ),
            CommonText("记录人：${item.leaveRegistrantName}", textSize: 16),
            Container(
               height: 15
            ),
            CommonText("外出事由：${item.leaveReasonName}", textSize: 16),
            Container(
               height: 15
            ),
            CommonText("联系电话：${item.accompanyMobile}", textSize: 16),
            Container(
               height: 15
            ),
            CommonText("陪同人：${item.accompanyName}", textSize: 16),

            Container(
               height: 15
            ),
            CommonText("预计销假时间：${item.planBackTime}", textSize: 16),
            Container(
               height: 15
            ),
            CommonText("请假外出时间：${item.leaveTime}", textSize: 16),

            IconTitleTextWidget("销假时间",hintText: "请选择",isRequired: true,text: backTime,padding: EdgeInsets.zero,).onTap(() {
              DatePicker.showDateTimePicker(context,currentTime: DateTime.parse(backTime),onConfirm: (date){
                backTime = date?.toDateStr(format)??"";
                getLeaveDays();
              });
            }),

            CommonText("请假天数：${leaveDays}天", textSize: 16),
            Container(
               height: 15
            ),
            CommonText('备注：'),
            Container(height: 10,),
            CommonInput(
              border: InputBorder.none,
              padding: EdgeInsets.zero,
              isDense: true,
              maxLines: 5,
              hintText: '请输入备注信息',
              onChanged: (str){
                remark = str;
              },
            ).addToContainer(
              padding: EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: Colours.bg_color,
                borderRadius: 10.borderRadius,
                border: Border.all(color: Colors.grey[200]!)
              )
            )
          ],
        ).addToContainer(
          margin: EdgeInsets.symmetric(horizontal: 16,vertical: 16)
        ).setWeight(1),

        Gaps.dividerLine.addToContainer(margin: EdgeInsets.symmetric(vertical: 16)),
        ElevatedButton(onPressed: (){
          confirmLeave();
        }, child: CommonText('确定',textColor: Colors.white),
        style: ButtonStyle(
          minimumSize: WidgetStateProperty.all(Size(MediaQuery.of(context).size.width-32, 45))
        ),),
        Container(height: 16,),
      ],
    );
  }

  Future<void> getLeaveDays() async {
    showLoadingDialog();
    try{
      var days = await leaveModel.calculateLeaveDays(item.leaveTime??"", backTime);
      hideDialog();
      leaveDays = days;
      notifyListeners();
    }catch(e){
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }



  Future<void> confirmLeave() async {
    showLoadingDialog(msg: '提交中...');
    try{
      var data = await leaveModel.confirmLeave(id: item.id.toString(),backTime: backTime,backRemark: remark,leaveDay: leaveDays);
      hideDialog();
      sendMessage(SendMessageEvent(MessageConfig.FINISH_LEAVE_SIGN));
      finish();
    }catch(e){
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }
}